package myThread;

import java.util.Random;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class demo29 {
    public static void main(String[] args) {
        BlockingDeque<Integer> blockingDeque = new LinkedBlockingDeque<>();
        Thread producer = new Thread(()->{
            Random random = new Random();
            while(true) {

                try {
                    int n = random.nextInt(1000);
                    System.out.println("生产元素" + n);
                    blockingDeque.put(n);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        },"生产者");
        Thread consumer = new Thread(()->{
            while(true){

                try {
                    int value = blockingDeque.take();
                    System.out.println("消费元素" + value);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        },"消费者");
        consumer.start();
        producer.start();
    }

}
